package net.zjitc.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/11/13.
 */
public class MyFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("myFilter被访问");
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();
        Object admin = session.getAttribute("admin");
        if(admin==null)
        {
            req.setAttribute("msg","你还没登陆，请先登陆");
            req.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
    public void destroy() {

    }
}
